package com.promis.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;

/**
 * Created by Administrator on 2017/8/10.
 */

public class DrawableUtils {

    public static final Drawable getDrawable(Context context, int resId) {
        final int version = Build.VERSION.SDK_INT;
        if (version >= 21) {
            return ContextCompat.getDrawable(context, resId);
        } else {
            return context.getResources().getDrawable(resId);
        }
    }

    public static StateListDrawable getStateListDrawable(Context context, int state, Drawable selectedDrawable, Drawable unselectDrawable) {
        StateListDrawable drawable = new StateListDrawable();
        int[][] states = new int[2][];
        states[0] = new int[] {-state};
        states[1] = new int[] {state};

        drawable.addState(states[0], unselectDrawable);
        drawable.addState(states[1], selectedDrawable);
        return drawable;
    }

    public static Drawable getDrawableFromAsset(Context context, String fileName) {
        Bitmap bitmap = BitmapUtils.getBitmapFromAsset(context, fileName);
        return new BitmapDrawable(context.getResources(), bitmap);
    }
}
